package com.miaoxun.dao.provider;

import org.apache.ibatis.jdbc.SQL;

public class MenuProvider {
    /**
     * 每个用户的菜单列表
     * @param id 用户id
     * @return 返回值
     */
    public String listMenuByUserId(Long id) {
        String[] columns = {"m.menu_id menuId", " parent_id parentId",
                "name", "url", "perms", "type", "icon", "order_num", "gmt_create gmtCreate",
                "gmt_modified gmtModified"};
        SQL sql = new SQL();
        sql.FROM("menu m");
        sql.SELECT_DISTINCT(columns);
        sql.INNER_JOIN("role_menu rm on  m.menu_id = rm.menu_id");
        sql.INNER_JOIN("user_role ur on rm.role_id =ur.role_id ");
        sql.WHERE(" ur.user_id = " + id);
        sql.AND().WHERE("m.type in(0,1)");
        sql.ORDER_BY("m.order_num");
        return sql.toString();
    }

    /**
     * 每个用户的访问权限
     * @param id 用户id
     * @return 返回sql
     */

    public String listUserPerms(Long id) {
        SQL sql=new SQL().SELECT_DISTINCT("m.perms");
        sql.FROM("menu m");
        sql.LEFT_OUTER_JOIN("role_menu rm on m.menu_id = rm.menu_id");
        sql.LEFT_OUTER_JOIN(" user_role ur on rm.role_id = ur.role_id");
        sql.WHERE("ur.user_id="+id);
        return sql.toString();
    }
}
